package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.SetmalDao;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：lintao
 * @date ：Created in 2020/12/4
 * @description ：统计报表服务接口实现类
 * @version: 1.0
 */
@Service
@Transactional
public class ReportServiceImpl implements ReportService {

    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private SetmalDao setmalDao;

    @Override
    public Map<String, Object> getBusinessReportData() throws Exception {
        // 获取当前日期
        String today = DateUtils.parseDate2String(DateUtils.getToday());
        // 获得本周一的日期
        String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        // 获取本月的第一天日期
        String firstDay4ThisMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());

        // 查询今日新增会员数
        Integer todayNewMember = memberDao.findCountByDate(today);
        // 查询总会员数
        Integer totalMember = memberDao.findCount();
        // 查询本周新增
        Integer thisWeekNewMember = memberDao.findCountByAfterDate(thisWeekMonday);
        // 查询本月新增
        Integer thisMonthNewMember = memberDao.findCountByAfterDate(firstDay4ThisMonth);
        // 今日预约会员数
        Integer todayOrderNumber = orderDao.findOrderCountByDate(today);
        // 今日到诊人数
        Integer todayVisitsNumber = orderDao.findOrderStatusCountByDate(today);
        // 本周预约人数
        Integer thisWeekOrderNumber = orderDao.findOrderCountByAfterDate(thisWeekMonday);
        // 本周到诊人数
        Integer thisWeekVisitsNumber = orderDao.findOrderStatusCountByAfterDate(thisWeekMonday);
        // 本月预约人数
        Integer thisMonthOrderNumber = orderDao.findOrderCountByAfterDate(firstDay4ThisMonth);
        // 本月到诊人数
        Integer thisMonthVisitsNumber = orderDao.findOrderStatusCountByAfterDate(firstDay4ThisMonth);
        // 热门套餐,取前2
        List<Map<String,Object>> list = setmalDao.findHotSetmeal();

        Map<String,Object> map = new HashMap<>();
        map.put("reportDate",today);
        map.put("todayNewMember",todayNewMember);
        map.put("totalMember",totalMember);
        map.put("thisWeekNewMember",thisWeekNewMember);
        map.put("thisMonthNewMember",thisMonthNewMember);
        map.put("todayOrderNumber",todayOrderNumber);
        map.put("thisWeekOrderNumber",thisWeekOrderNumber);
        map.put("thisMonthOrderNumber",thisMonthOrderNumber);
        map.put("todayVisitsNumber",todayVisitsNumber);
        map.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        map.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
        map.put("hotSetmeal",list);
        return map;
    }
}
